<?php
include_once "class/Classes.php";

$task = empty($_REQUEST['task'])?"":$_REQUEST['task'];
error_log($task);

switch($task){
	case "saveAccessMatrix":
		saveAccessMatrix();
		break;
	case "createUser";
		createUser();
		break;
	case "saveFeedback":
		saveFeedback();
		break;
	case "deleteFeedback":
		deleteFeedback();
		break;
	case "addNewFeedback":
		addNewFeedback();
		break;
	case "addNewClass":
		addNewClass();
		break;
	case "saveSubjectDesc":
		saveSubjectDesc();
		break;
	case "addNewSubject":
		addNewSubject();
		break;
	case "saveDepartmentPic":
		saveDepartmentPic();
		break;
	case "saveDepartmentDesc":
		saveDepartmentDesc();
		break;
	case "deleteSubject":
		deleteSubject();
		break;
	case "deleteDepartment":
		deleteDepartment();
		break;
	case "addNewDepartment":
		addNewDepartment();
		break;
	case "saveFacultyPic":
		saveFacultyPic();
		break;
	case "saveFacultyDesc":
		saveFacultyDesc();
		break;
	case "deleteFaculty":
		deleteFaculty();
		break;
	case "addNewFaculty";
		addNewFaculty();
		break;
	case "deleteNotice":
		deleteNotice();
		break;
	case "getNotice":
		getNotice();
		break;
	case "saveNewNotice";
		saveNewNotice();
		break;
	case "addNewNotice":
		addNewNotice();
		break;
	case "addNewSemester":
		addNewSemester();
		break;
	case "getPresidentCandidates";
		getPresidentCandidates();
		break;
	case "saveSchoolPic":
		saveSchoolPic();
		break;
	case "saveSchoolDesc":
		saveSchoolDesc();
		break;
	case "saveSemester":
		saveSemester();
		break;
	case "currentsemester":
		handleCurrentSemester();
		break;
	default:
		error_log("Task not found.");
		echo "Task not found";
}
//echo "</pre>";

function saveAccessMatrix(){
	$task = $_REQUEST['scope'];
	$action = $_REQUEST['action'];
	$level = $_REQUEST['level'];
	$statement = "minimumlevel = $level";
	$condition = "task='$task' AND name='$action'";
	$accessmatrixdao = new AccessMatrixDAO();
	echo $accessmatrixdao->updateList($statement, $condition);
}

function createUser(){
	$group = $_REQUEST['group'];
	$name = $_REQUEST['name'];
	$login = $_REQUEST['login'];
	$password = $_REQUEST['password'];
	$schoolid = $_REQUEST['schoolid'];
	
	$userdao = new UserDAO();
	$user = new User();
	$user->setName($name);
	$user->setLogin($login);
	$user->setPassword($password);
	$user->setSchoolid($schoolid);
	$user->setGid($group);
	$userdao->add($user);
	echo $user->getId();
}

function saveFeedback(){
	$id = $_REQUEST['id'];
	$subject = $_REQUEST['subject'];
	$content = $_REQUEST['content'];
	$pageid = $_REQUEST['pageid'];
	$feedbackdao = new FeedbackDAO();
	$feedback = $feedbackdao->get($id);
	$feedback->setSubject($subject);
	$feedback->setContent($content);
	$feedbackdao->save($feedback);
	echo "{";
	echo "\"id\":\"$id\",";
	echo "\"subject\":\"$subject\",";
	echo "\"content\":\"$content\",";
	echo "\"pageid\":\"$pageid\"";
	echo "}";
}

function deleteFeedback(){
	$id = $_REQUEST['id'];
	$feedbackdao = new FeedbackDAO();
	$feedback = $feedbackdao->get($id);
	$feedbackdao->delete($feedback);
	echo "1";
}

function addNewFeedback() {
	$subject = $_REQUEST['subject'];
	$content = $_REQUEST['content'];
	$pageid = $_REQUEST['pageid'];
	$referenceid = $_REQUEST['referenceid'];
	
	$feedback = new Feedback();
	$feedback->setSubject($subject);
	$feedback->setContent($content);
	$feedback->setPageid($pageid);
	$feedback->setReferenceid($referenceid);
	
	$feedbackdao = new FeedbackDAO();
	$feedbackdao->add($feedback);
	$id = $feedback->getId();
	echo "{";
	echo "\"id\":\"$id\",";
	echo "\"subject\":\"$subject\",";
	echo "\"content\":\"$content\",";
	echo "\"pageid\":\"$pageid\"";
	echo "}";
	
}

function addNewClass(){
	$lecturer = $_REQUEST['lecturer'];
	$description = $_REQUEST['description'];
	$weekday = $_REQUEST['weekday'];
	$fromhour = $_REQUEST['fromhour'];
	$fromminutes = $_REQUEST['fromminutes'];
	$fromampm = $_REQUEST['fromampm'];
	$tohour = $_REQUEST['tohour'];
	$tominutes = $_REQUEST['tominutes'];
	$toampm = $_REQUEST['toampm'];
	$location = $_REQUEST['location'];
	$subjectid = $_REQUEST['subjectid'];
	$semesterid = $_REQUEST['semesterid'];

	$starttime = ( $fromampm == 'am' ? $fromhour : $fromhour + 12 ) . ':' . $fromminutes . ':00';
	$endtime   = ( $toampm == 'am' ? $tohour : $tohour + 12 ) . ':' . $tominutes . ':00';
	
	$timetable = new Timetable();
	$timetable->setWeekday($weekday);
	$timetable->setLocation($location);
	$timetable->setStarttime($starttime);
	$timetable->setEndtime($endtime);
	
	$clazz = new Clazz();
	$clazz->setPic($lecturer);
	$clazz->setDescription($description);
	$clazz->setSemesterid($semesterid);
	$clazz->setSubjectid($subjectid);
	$clazz->setTimetable($timetable);
	
	$clazzdao = new ClazzDAO();
	$clazzdao->add($clazz);
	
	echo $clazz->getId();
}

function saveSubjectDesc(){
	$id = $_REQUEST['id'];
	$description = $_REQUEST['description'];
	$description = str_replace("'", "\'", $description);
	$name = $_REQUEST['name'];
	$subjectdao = new SubjectDAO();
	$i = $subjectdao->updateList("description='$description', name='$name'", "id=$id");
	echo $i;
}

function addNewSubject(){
	$name = $_REQUEST['name'];
	$departmentid = $_REQUEST['departmentid'];
	$description = $_REQUEST['description'];
	
	$subject = new Subject();
	$subject->setName($name);
	$subject->setDescription($description);
	$subject->setDepartmentid($departmentid);
	$subjectdao = new SubjectDAO();
	$subjectdao->add($subject);
	echo $subject->getId();
}

function saveDepartmentPic(){
	$id = $_REQUEST['id'];
	$departmentid = $_REQUEST['departmentid'];
	$departmentdao = new DepartmentDAO();
	$i = $departmentdao->updateList("pic=$id", "id=$departmentid");
	echo $i;	
}

function saveDepartmentDesc(){
	$id = $_REQUEST['id'];
	$description = $_REQUEST['description'];
	$description = str_replace("'", "\'", $description);
	$name = $_REQUEST['name'];
	$departmentdao = new DepartmentDAO();
	$i = $departmentdao->updateList("description='$description', name='$name'", "id=$id");
	echo $i;
}

function deleteSubject(){
	$id = $_REQUEST['id'];
	$subjectdao = new SubjectDAO();
	$subjeect = $subjectdao->get($id);
	$subjectdao->delete($subjeect);
	echo "1";	
}

function deleteDepartment(){
	$id = $_REQUEST['id'];
	$departmentdao = new DepartmentDAO();
	$department = $departmentdao->get($id);
	$departmentdao->delete($department);
	echo "1";	
}

function addNewDepartment(){
	$name = $_REQUEST['name'];
	$facultyid = $_REQUEST['facultyid'];
	$description = $_REQUEST['description'];
	$pic = $_REQUEST['pic'];
	
	$department = new Department();
	$department->setName($name);
	$department->setDescription($description);
	$department->setFacultyid($facultyid);
	$department->setPic($pic);
	$departmentdao = new DepartmentDAO();
	$departmentdao->add($department);
	echo $department->getId();
}

function saveFacultyPic(){
	$id = $_REQUEST['id'];
	$facultyid = $_REQUEST['facultyid'];
	$facultydao = new FacultyDAO();
	$i = $facultydao->updateList("pic=$id", "id=$facultyid");
	echo $i;	
}

function saveFacultyDesc(){
	$id = $_REQUEST['id'];
	$description = $_REQUEST['description'];
	$description = str_replace("'", "\'", $description);
	$name = $_REQUEST['name'];
	error_log( "<h1>Cool $id $name</h1>" );
	$facultydao = new FacultyDAO();
	$i = $facultydao->updateList("description='$description', name='$name'", "id=$id");
	echo $i;
}

function addNewSemester(){
	$name = $_REQUEST['name'];
	$year = $_REQUEST['year'];
	$fromdate = $_REQUEST['fromdate'];
	$todate = $_REQUEST['todate'];
	$schoolid = $_REQUEST['id'];
	$sem = new Semester();
	$sem->setName($name);
	$sem->setYear($year);
	$sem->setFromdate($fromdate);
	$sem->setTodate($todate);
	$sem->setSchoolid($schoolid);
	$semdao = new SemesterDAO();
	$semdao->add($sem);
	// update session
	$_SESSION['currsemester'] = $sem;
}

function saveSchoolPic(){
	$id= $_REQUEST['id'];
	$schoolid= $_REQUEST['schoolid'];
	$schooldao = new SchoolDAO();
	$schooldao->updateList("pic=$id", "id=$schoolid");
}

function saveSchoolDesc(){
	$id = $_REQUEST['id'];
	$description = $_REQUEST['description'];
	$description = str_replace("'", "\'", $description);
	$schooldao = new SchoolDAO();
	$schooldao->updateList("description='$description'", "id=$id");
}

function saveSemester(){
	$id = $_REQUEST['id'];
	$name = $_REQUEST['name'];
	$year = $_REQUEST['year'];
	$fromdate = $_REQUEST['fromdate'];
	$todate = $_REQUEST['todate'];
	$semester = new Semester();
	$semester->setId($id);
	$semester->setName($name);
	$semester->setYear($year);
	$semester->setFromdate($fromdate);
	$semester->setTodate($todate);
	$semesterdao = new SemesterDAO();
	$semesterdao->save($semester);
//	echo "$id:$name:$year:$fromdate:$todate";
}

function getPresidentCandidates(){
	$id = $_REQUEST['id'];
	$userdao = new UserDAO();
	$list = $userdao->getList("schoolid=$id AND gid<5");
	echo "{ \"candidates\": [";
	for($i=0; $i<sizeof($list); $i++){
		$uid = $list[$i]->getId();
		$uname = $list[$i]->getName();
		echo "{\"id\": \"$uid\", \"name\": \"$uname\" }";
		if($i<sizeof($list)-1){
			echo ",";
		}
	}
	echo " ] }";
}

function handleCurrentSemester(){
	$id = $_REQUEST['id'];
	$currentsemester = new Semester();
	$semesterdao = new SemesterDAO();
	$list = $semesterdao->getList("schoolid=" . $id . " order by id desc limit 1");
//	{"a":1,"b":2,"c":3,"d":4,"e":5}
	if(sizeof($list)>0){
		$currentsemester = $list[0];
//		print_r($currentsemester);
		echo "{";
		echo "\"id\":\"" . $currentsemester->getId() . "\",";
		echo "\"name\":\"" . $currentsemester->getName() . "\",";
		echo "\"year\":\"" . $currentsemester->getYear() . "\",";
		echo "\"fromdate\":\"" . $currentsemester->getFromdate() . "\",";
		echo "\"todate\":\"" . $currentsemester->getTodate() . "\"";
		echo "}";
//		echo json_encode( $currentsemester , JSON_FORCE_OBJECT);
	} else {
		echo '{}';
	}		
}

function addNewNotice(){
	$subject = $_REQUEST['subject'];
	$content = $_REQUEST['content'];
	$pageid = $_REQUEST['pageid'];
	$schoolid = $_REQUEST['schoolid'];
	$referenceid = $_REQUEST['referenceid'];
	
	$notice = new Notice();
	$notice->setSubject($subject);
	$notice->setContent($content);
	$notice->setPageid($pageid);
	$notice->setSchoolid($schoolid);
	$notice->setReferenceid($referenceid);
	
	$noticedao = new NoticeDAO();
	$noticedao->add($notice);
	$id = $notice->getId();
	echo "{";
	echo "\"id\":\"$id\",";
	echo "\"subject\":\"$subject\",";
	echo "\"content\":\"$content\",";
	echo "\"pageid\":\"$pageid\",";
	echo "\"schoolid\":\"$schoolid\"";
	echo "}";
}

function saveNewNotice() {
	$id = $_REQUEST['id'];
	$subject = $_REQUEST['subject'];
	$content = $_REQUEST['content'];
	$pageid = $_REQUEST['pageid'];
	$notice = new Notice();
	$notice->setId($id);
	$notice->setSubject($subject);
	$notice->setContent($content);
	$notice->setPageid($pageid);
	$noticedao = new NoticeDAO();
	$noticedao->save($notice);
	echo "{";
	echo "\"id\":\"$id\",";
	echo "\"subject\":\"$subject\",";
	echo "\"content\":\"$content\",";
	echo "\"pageid\":\"$pageid\"";
	echo "}";
}

function getNotice(){
	$id = $_REQUEST['id'];
	$notice = new Notice();
	$noticedao = new NoticeDAO();
	$notice = $noticedao->get($id);
	$subject = $notice->getSubject();
	$content = $notice->getContent();
	$pageid = $notice->getPageid();
	echo "{";
	echo "\"id\":\"$id\",";
	echo "\"subject\":\"$subject\",";
	echo "\"content\":\"$content\",";
	echo "\"pageid\":\"$pageid\"";
	echo "};";
}

function deleteNotice() {
	$id = $_REQUEST['id'];
	$noticedao = new NoticeDAO();
	$notice = $noticedao->get($id);
	$noticedao->delete($notice);
	echo "1";
}

function addNewFaculty() {
	$name=$_REQUEST['name'];
	$description = $_REQUEST['description'];
	$description = str_replace("'", "\'", $description);
	$pic = $_REQUEST['pic'];
	$schoolid = $_REQUEST['schoolid'];
	$faculty = new Faculty();
	$facultydao = new FacultyDAO();
	$faculty->setName($name);
	$faculty->setDescription($description);
	$faculty->setPic($pic);
	$faculty->setSchoolid($schoolid);
	$facultydao->add($faculty);
	echo $faculty->getId();
}

function deleteFaculty() {
	$id = $_REQUEST['id'];
	$facultydao = new FacultyDAO();
	$faculty = $facultydao->get($id);
	$facultydao->delete($faculty);
	echo "1";	
}

?>